package com.elf.bluetoothtest.widget;

import android.bluetooth.BluetoothGattCharacteristic;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;

import com.elf.bluetoothtest.databinding.WidgetCharacteristicBinding;

import java.util.ArrayList;
import java.util.List;

public class CharacteristicWidget extends AbsFrameLayout {

    private WidgetCharacteristicBinding binding;
    private BluetoothGattCharacteristic characteristic;

    public CharacteristicWidget(Context context) {
        super(context);
    }

    public CharacteristicWidget(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void initView(AttributeSet attrs) {
        binding = WidgetCharacteristicBinding.inflate(LayoutInflater.from(getContext()), this, true);
    }

    public void setData(BluetoothGattCharacteristic characteristic) {
        this.characteristic = characteristic;
        binding.tvUuid.setText("uuid:" + characteristic.getUuid().toString().toUpperCase());

        List<String> properties = new ArrayList<>();
        int charaProp = characteristic.getProperties();
        if ((charaProp & BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
            // Log.e("nihao","gattCharacteristic的属性为:  可读");
            properties.add("READ");
        }
        if ((charaProp & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
            // Log.e("nihao","gattCharacteristic的属性为:  可写");
            properties.add("WRITE");
        }
        if ((charaProp & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
            // Log.e("nihao","gattCharacteristic的属性为:  具备通知属性");
            properties.add("NOTIFY");
        }
        binding.tvProperties.setText("properties:" + properties.toString().replace("[", "").replace("]", ""));
    }

    public BluetoothGattCharacteristic getCharacteristic(){
        return characteristic;
    }
}
